export const state = useStorage('language', '');

/**
 * 获取浏览器语言
 * @param messages en: {json} json  locale 里读取的键值对
 * @returns {string}
 */
export function getLanguage(messages) {
  if (state.value) {
    return state.value;
  }
  const language = (navigator.language || navigator.browserLanguage).toLowerCase();
  const locales = Object.keys(messages);
  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      state.value = locale;
      return locale;
    }
  }
  state.value = 'en';
  return 'en';
}

/**
 *  获取静态资源
 * @param url
 * @returns {string}
 */
export function useAssets(url) {
  return new URL(`../assets/${url}`, import.meta.url).href;
}
